package com.vtuzx.kingdee.xqc_api.web;

import com.vtuzx.core.bean.dto.VtuzxToken;
import com.vtuzx.core.constant.IVtuzxConst;
import com.vtuzx.core.utils.VtuzxUtil;
import com.vtuzx.kingdee.xqc_api.service.IXqcInventoryCheckService;
import com.vtuzx.web.validator.VtuzxValidator;
import com.vtuzx.web.web.VtuzxApi;
import com.vtuzx.web.web.VtuzxControlParam;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 库存
 */
@RequestMapping("/${vtuzx.fast.api.path}/${vtuzx.fast.api.version}/inventory_check")
@RestController
public class XqcInventoryCheckController {

    /** 库存服务 */
    @Autowired
    private IXqcInventoryCheckService inventoryCheckService;

    /**
     * 获取用户盘点作业列表
     */
    @PostMapping("/get_user_inventory_check_task")
    @VtuzxApi
    public Object getUserInventoryCheckTask(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String type = VtuzxUtil.getString(param, "type");
        return inventoryCheckService.getUserInventoryCheckTaskList(_userId, type);
    }

    /**
     * 获取盘点作业物料列表
     */
    @PostMapping("/get_inventory_check_task_detail_list")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "taskId", label = "作业", notNull = true, objectId = true)
    } )
    public Object getInventoryCheckTaskDetailList(@RequestBody Map<String, Object> param) {
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String taskId = VtuzxUtil.getString(param, "taskId");
        return inventoryCheckService.getInventoryCheckTaskDetailList(stockNo, locationNo, taskId);
    }

    /**
     * 保存盘点数(箱号 + 数量)
     */
    @PostMapping("/add_inventory_check_result")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "taskId", label = "作业", notNull = true, objectId = true),
            @VtuzxValidator(key = "materialNo", label = "物料编码", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object addInventoryCheckResult(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String taskId = VtuzxUtil.getString(param, "taskId");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        String count = VtuzxUtil.getString(param, "count");
        return inventoryCheckService.addInventoryCheckResult(_userId, stockNo, locationNo, taskId, materialNo, boxNo, count);
    }

    /**
     * 编辑盘点数(数量)
     */
    @PostMapping("/update_inventory_check_result")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "taskId", label = "作业", notNull = true, objectId = true),
            @VtuzxValidator(key = "materialNo", label = "物料编码", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true),
            @VtuzxValidator(key = "count", label = "数量", notNull = true, isNumber = true)
    } )
    public Object updateInventoryCheckResult(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String taskId = VtuzxUtil.getString(param, "taskId");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        String count = VtuzxUtil.getString(param, "count");
        return inventoryCheckService.updateInventoryCheckResult(_userId, stockNo, locationNo, taskId, materialNo, boxNo, count);
    }

    /**
     * 删除盘点(箱号)
     */
    @PostMapping("/delete_inventory_check_material_box")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "taskId", label = "作业", notNull = true, objectId = true),
            @VtuzxValidator(key = "materialNo", label = "物料编码", notNull = true),
            @VtuzxValidator(key = "boxNo", label = "箱号", notNull = true)
    } )
    public Object deleteInventoryCheckMaterialBox(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String taskId = VtuzxUtil.getString(param, "taskId");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        String boxNo = VtuzxUtil.getString(param, "boxNo");
        inventoryCheckService.deleteInventoryCheckMaterialBox(_userId, stockNo, locationNo, taskId, materialNo, boxNo);
        return null;
    }

    /**
     * 删除盘点(物料)
     */
    @PostMapping("/delete_inventory_check_material")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "locationNo", label = "仓位", notNull = true),
            @VtuzxValidator(key = "taskId", label = "作业", notNull = true, objectId = true),
            @VtuzxValidator(key = "materialNo", label = "物料编码", notNull = true)
    } )
    public Object deleteInventoryCheckMaterial(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String taskId = VtuzxUtil.getString(param, "taskId");
        String materialNo = VtuzxUtil.getString(param, "materialNo");
        inventoryCheckService.deleteInventoryCheckMaterial(_userId, stockNo, locationNo, taskId, materialNo);
        return null;
    }

    /**
     * 保存盘点结果
     */
    @PostMapping("/save_inventory_check")
    @VtuzxApi( fields = {
            @VtuzxValidator(key = "stockNo", label = "仓库", notNull = true),
            @VtuzxValidator(key = "taskId", label = "作业", notNull = true, objectId = true)
    } )
    public Object saveInventoryCheck(@VtuzxControlParam VtuzxToken token, @RequestBody Map<String, Object> param) {
        ObjectId _userId = token.getObject(IVtuzxConst.Key__targetId);
        String stockNo = VtuzxUtil.getString(param, "stockNo");
        String locationNo = VtuzxUtil.getString(param, "locationNo");
        String taskId = VtuzxUtil.getString(param, "taskId");
        inventoryCheckService.saveInventoryCheck(_userId, stockNo, locationNo, taskId);
        return null;
    }
}
